
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.FreightDetailDao;
import com.sun.showcase.biz.mall.service.FreightDetailService;
import com.sun.showcase.client.domain.mall.FreightDetail;
import com.sun.showcase.client.query.mall.FreightDetailQuery;
import com.sun.showcase.client.vo.FreightFeeQueryEntity;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ArithUtil;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("freightDetailService")
@Transactional
public class FreightDetailServiceImpl implements FreightDetailService{
	@Autowired
	private FreightDetailDao freightDetailDao;
	
	public void setFreightDetailDao(FreightDetailDao dao) {
		this.freightDetailDao = dao;
	}

	public DataGrid datagrid(FreightDetailQuery freightDetailQuery) {
		DataGrid j = new DataGrid();
		Pager<FreightDetail> pager  = find(freightDetailQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<FreightDetailQuery> getQuerysFromEntitys(List<FreightDetail> freightDetails) {
		List<FreightDetailQuery> freightDetailQuerys = new ArrayList<FreightDetailQuery>();
		if (freightDetails != null && freightDetails.size() > 0) {
			for (FreightDetail tb : freightDetails) {
				FreightDetailQuery b = new FreightDetailQuery();
				BeanUtils.copyProperties(tb, b);
				freightDetailQuerys.add(b);
			}
		}
		return freightDetailQuerys;
	}

	private Pager<FreightDetail> find(FreightDetailQuery freightDetailQuery) {
		Page<FreightDetail> page = PageHelper.startPage(freightDetailQuery.getPage().intValue(), freightDetailQuery.getRows().intValue());
		List<FreightDetail> list = freightDetailDao.findList(freightDetailQuery);
		Pager<FreightDetail> pager = new Pager<FreightDetail>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public FreightDetail add(FreightDetailQuery freightDetailQuery) {
		FreightDetail t = new FreightDetail();
		BeanUtils.copyProperties(freightDetailQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		freightDetailDao.save(t);
		freightDetailQuery.setId(t.getId());
		return t;
		
	}

	public void update(FreightDetailQuery freightDetailQuery) {
		FreightDetail t = freightDetailDao.getById(freightDetailQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(freightDetailQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	freightDetailDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(FreightDetailQuery freightDetailQuery) {
	    freightDetailDao.updatePart(freightDetailQuery);
	}

	public void delete(String[] ids) {
		freightDetailDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		freightDetailDao.deletePt(ids);
	}

	public FreightDetail get(FreightDetailQuery freightDetailQuery) {
		return freightDetailDao.getById(freightDetailQuery.getId());
	}

	public FreightDetail get(String id) {
		return freightDetailDao.getById(id);
	}

	
	public List<FreightDetailQuery> listAll(FreightDetailQuery freightDetailQuery) {
	    List<FreightDetail> list = freightDetailDao.findList(freightDetailQuery);
		List<FreightDetailQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public Long getExistRangeCount(FreightDetailQuery freightDetailQuery) {
		return freightDetailDao.getExistRangeCount(freightDetailQuery);
	}

	@Override
	public FreightDetail getFreight(String freightId, Double totalPayAmount) {
		return freightDetailDao.getFreight(freightId, totalPayAmount);
	}

	@Override
	public Double getFreightFee(List<FreightFeeQueryEntity> info) {
		Double res = 0D;
		for(FreightFeeQueryEntity  e : info) {
			FreightDetail f = this.getFreight(e.getFreightId(), e.getAmount());
			if(f !=null ) {
				res = ArithUtil.add(res, f.getFreightExpense());
			}
		}
		return res;
	}
	
	
}
